package cn.springsoter.core.tool.exception;

import cn.springsoter.core.tool.api.IResultCode;
import cn.springsoter.core.tool.api.ResultCode;
import io.sentry.Sentry;
import lombok.Getter;


/**
 * 业务异常
 *
 * @author kevin
 */
public class ServiceException extends RuntimeException {
    private static final long serialVersionUID = 2359767895161832954L;

    @Getter
    private final IResultCode resultCode;

    public ServiceException(String message) {
        super(message);
        this.resultCode = ResultCode.FAILURE;
        Sentry.captureException(this);
    }

    public ServiceException(IResultCode resultCode) {
        super(resultCode.getMessage());
        this.resultCode = resultCode;
        Sentry.captureException(this);
    }

    public ServiceException(IResultCode resultCode, Throwable cause) {
        super(cause);
        this.resultCode = resultCode;
        Sentry.captureException(this);
    }

    public ServiceException(IResultCode resultCode, String message) {
        super(message);
        this.resultCode = resultCode;
        Sentry.captureException(this);
    }


    /**
     * 未授权
     *
     * @return
     */
    public static ServiceException ofUnauthorized() {
        return new ServiceException(ResultCode.UN_AUTHORIZED);
    }

    /**
     * 没有权限，拒绝访问
     *
     * @return
     */
    public static ServiceException ofForbidden() {
        return new ServiceException(ResultCode.REQ_REJECT);
    }

    /**
     * 数据没找到/不存在
     *
     * @return
     */
    public static ServiceException ofNotFound() {
        return new ServiceException(ResultCode.NOT_FOUND);
    }

    /**
     * 参数为空或者不正确
     *
     * @return
     */
    public static ServiceException ofParamValidError() {
        return new ServiceException(ResultCode.PARAM_VALID_ERROR);
    }

    /**
     * 参数缺失
     *
     * @return
     */
    public static ServiceException ofParamMissing() {
        return new ServiceException(ResultCode.PARAM_MISS);
    }

    /**
     * 提高性能
     *
     * @return Throwable
     */
    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    public Throwable doFillInStackTrace() {
        return super.fillInStackTrace();
    }

}
